//
//  PageContentView.m
//  集成ReactNative
//
//  Created by Admin on 2017/3/6.
//  Copyright © 2017年 Admin. All rights reserved.
//

#import "PageContentView.h"

@interface PageContentView () <UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong) NSMutableArray <UIViewController *> *childVCs;
@property (nonatomic,weak) UIViewController *parentViewController;
@property (nonatomic,assign) CGFloat startOffsetX;
@property (nonatomic,assign) BOOL isForbidScrollDelegate;
@property (nonatomic,strong) UICollectionView *collectionView;
@end

@implementation PageContentView
static NSString *ContentCellID = @"ContentCellID";
#pragma mark - 懒加载
-(UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = self.bounds.size;
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.showsHorizontalScrollIndicator = false;
        _collectionView.pagingEnabled = true;
        _collectionView.bounces = false;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.scrollsToTop = false;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ContentCellID];
    }
    return _collectionView;
}

- (instancetype)initWithFrame:(CGRect)frame childVCs:(NSMutableArray *)childVCs parentViewController:(UIViewController *)parentViewController {
    self = [super initWithFrame:frame];
    if (self) {
        self.childVCs = childVCs;
        self.parentViewController = parentViewController;
        self.isForbidScrollDelegate = false;
        self.startOffsetX = 0;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    // 1.将所有的子控制器添加父控制器中
    for (UIViewController *childVCs in self.childVCs) {
        [self.parentViewController addChildViewController:childVCs];
    }
    [self addSubview:self.collectionView];
    self.collectionView.frame = self.bounds;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.childVCs.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    // 1.创建Cell
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ContentCellID forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UICollectionViewCell alloc] init];
    }
    // 2.给Cell设置内容
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    UIViewController *childVC = self.childVCs[indexPath.item];
    childVC.view.frame = cell.contentView.frame;
    [cell.contentView addSubview:childVC.view];
    return cell;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.isForbidScrollDelegate = false;
    self.startOffsetX = scrollView.contentOffset.x;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // 0.判断是否是点击事件
    if (self.isForbidScrollDelegate) return;
    
    // 1.定义获取需要的数据
    CGFloat progress = 0;
    NSInteger sourceIndex = 0;
    NSInteger targetIndex = 0;
    
    // 2.判断是左滑还是右滑
    CGFloat currentOffsetX = scrollView.contentOffset.x;
    CGFloat scrollViewW = scrollView.bounds.size.width;
    if (currentOffsetX > self.startOffsetX) { // 左滑
        // 1.计算progress
        progress = currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW);
        // 2.计算sourceIndex
        sourceIndex = (NSInteger)(currentOffsetX / scrollViewW);
        // 3.计算targetIndex
        targetIndex = sourceIndex + 1;
        if (targetIndex >= self.childVCs.count) {
            targetIndex = self.childVCs.count - 1;
        }
        // 4.如果完全划过去
        if (currentOffsetX - self.startOffsetX == scrollViewW) {
            progress = 1;
            targetIndex = sourceIndex;
        }
    } else {
        progress = 1 - (currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW));
        
        // 2.计算targetIndex
        targetIndex = (NSInteger)(currentOffsetX / scrollViewW);
        
        // 3.计算sourceIndex
        sourceIndex = targetIndex + 1;
        if (sourceIndex >= self.childVCs.count) {
            sourceIndex = self.childVCs.count - 1;
        }
    }
    // 3.将progress/sourceIndex/targetIndex传递给titleView
    if (self.delegate && [self.delegate respondsToSelector:@selector(pageContentView:Progress:sourceIndex:targetIndex:)]) {
        [self.delegate pageContentView:self Progress:progress sourceIndex:sourceIndex targetIndex:targetIndex];
    }
}

- (void)setCurrentIndex:(NSInteger)currentIndex {
    // 1.记录需要进制执行代理方法
    self.isForbidScrollDelegate = true;
    
    // 2.滚动正确的位置
    CGFloat offsetX = currentIndex * self.collectionView.frame.size.width;
    [self.collectionView setContentOffset:CGPointMake(offsetX, 0) animated:false];
}

@end
